package com.qbm.association.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qbm.association.bean.ResultBean;
import com.qbm.association.config.Audience;
import com.qbm.association.entity.Administrator;
import com.qbm.association.entity.Club;
import com.qbm.association.entity.Student;
import com.qbm.association.mapper.AdministratorMapper;
import com.qbm.association.service.AdministratorService;
import com.qbm.association.util.JWTTokenUtil;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Aron
 * @since 2022-02-07
 */
@RestController
@RequestMapping("/association/administrator")
public class AdministratorController {

    @Resource
    private AdministratorMapper administratorMapper;

    @Resource
    private AdministratorService administratorService;

    @Autowired
    private Audience audience;

    @GetMapping("getTeacherUserName")
    public ResultBean getTeacherUserName(HttpServletRequest request) {
        String name = administratorService.getByUsername(JWTTokenUtil.getUserName
                (request.getHeader(JWTTokenUtil.AUTH_HEAD_KEY), audience.getBase64Secret())).getName();
        return ResultBean.success(name, "获取成功");
    }

    @GetMapping("getPageAdminByName")
    public ResultBean getPageAdminByName(Integer currentPage, String name) {
        QueryWrapper<Administrator> queryWrapper = new QueryWrapper<>();
        if (name != null && name.length() != 0) {
            queryWrapper.like("name", name);
        }
        Page<Administrator> page = new Page<>(currentPage, 5);
        Page<Administrator> page1 = administratorMapper.selectPage(page, queryWrapper);
        return ResultBean.success(page1, "获取成功");
    }

    @GetMapping("getAdminById")
    public ResultBean getAdminById(String id) {
        QueryWrapper<Administrator> queryWrapper = new QueryWrapper<>();
        if (id != null && id.length() != 0) {
            queryWrapper.eq("id", id);
        }
        Administrator administrator = administratorMapper.selectOne(queryWrapper);
        return ResultBean.success(administrator, "获取成功");
    }

    @PostMapping("deleteAdmin")
    public ResultBean deleteAdmin(Administrator administrator) {
        boolean isTrue = administratorService.removeById(administrator);
        Assert.isTrue(isTrue, "删除失败");
        return ResultBean.success("删除成功");
    }

    @PostMapping("updateAdmin")
    public ResultBean updateAdmin(Administrator administrator) {
        boolean isTrue = administratorService.updateById(administrator);
        Assert.isTrue(isTrue, "更新失败");
        return ResultBean.success("更新成功");
    }

    @PostMapping("addAdmin")
//    @ResponseBody
    public ResultBean addAdmin(@RequestParam Map<String, Object> para) {
        QueryWrapper<Administrator> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", para.get("userName"));
        boolean isTrue = administratorService.list(queryWrapper).size() == 0;
        Assert.isTrue(isTrue,"该管理员已存在");
        administratorService.add(para);
        return ResultBean.success( "添加成功");
    }


}

